<%@ page contentType="text/html;charset=UTF-8" %>

<%
//monta o controlador de acordo com pessoa que está na session.
String controlador;
def estagiarioInstance = Estagiario.findById(session.pessoa.id);
if(estagiarioInstance != null){
    controlador = "estagiario";
}
def ucInstance = UnidadeConcedente.findById(session.pessoa.id);
if(ucInstance != null){
    controlador = "unidadeConcedente";
}
def ieInstance = InstituicaoEnsino.findById(session.pessoa.id);
if(ieInstance != null){
    controlador = "instituicaoEnsino";
}
%>

<h1 class="cabelistlateral">Endereços</h1>
<g:hasErrors bean="${enderecoInstance}">
<div class="errors">
    <g:renderErrors bean="${enderecoInstance}" as="list" />
</div>
</g:hasErrors>
<%
if(enderecoInstance?.id){
    %>
    <form method="post" action="${createLink(controller:'endereco', action:'update', id:enderecoInstance.id)}">
    <%
}else{
    %>
    <form method="post" action="${createLink(controller:'endereco', action:'save')}">
    <%
}
%>
    <g:hiddenField name="pessoa.id" value="${session.pessoa.id}" />
    <g:hiddenField name="tipo" value="${controlador}" />
    <div class="dialog">
        <table>
            <input type="hidden" name="psave" value="1"/>
            <tbody>
                <tr class="prop cadastrolateral">
                    <td valign="top" class="name" class="value ${hasErrors(bean: enderecoInstance, field: 'endereco', 'errors')}">
                        <label for="endereco"><g:message code="endereco.endereco.label" default="Endereco" /></label>
                        <br/>
                        <g:textField name="endereco" size="33" maxlength="100" value="${enderecoInstance?.endereco}" />
                    </td>
                    <td valign="top" class="name" class="value ${hasErrors(bean: enderecoInstance, field: 'bairro', 'errors')}">
                        <label for="bairro"><g:message code="endereco.bairro.label" default="Bairro" /></label>
                        <br/>
                        <g:textField name="bairro" maxlength="100" value="${enderecoInstance?.bairro}" />
                    </td>
                </tr>

                <tr class="prop cadastrolateral">
                    <td valign="top" class="name" class="value ${hasErrors(bean: enderecoInstance, field: 'complemento', 'errors')}">
                        <label for="complemento"><g:message code="endereco.complemento.label" default="Complemento" /></label>
                        <br/>
                        <g:textField name="complemento" size="33" maxlength="30" value="${enderecoInstance?.complemento}" />
                    </td>
                    <td valign="top" class="name" class="value ${hasErrors(bean: enderecoInstance, field: 'cep', 'errors')}">
                        <label for="cep"><g:message code="endereco.cep.label" default="Cep" /></label>
                        <br/>
                        <g:textField name="cep" maxlength="8" value="${enderecoInstance?.cep}" />
                    </td>
                </tr>

                <tr class="prop cadastrolateral">
                  <td colspan="2">
                    <table border="0" cellspacing="0" cellpadding="0">
                      <tr>
                          <td valign="top" class="name" class="value ${hasErrors(bean: enderecoInstance, field: 'estado', 'errors')}">
                              <label for="estado"><g:message code="endereco.estado.label" default="Estado" /></label>
                              <br/>
                              <g:select name="estado.id" from="${Estado.list()}" optionKey="id" value="${enderecoInstance?.cidade?.estado?.id}" optionValue="nome" />
                          </td>
                          <td valign="top" colspan="2" class="name" class="value ${hasErrors(bean: enderecoInstance, field: 'cidade', 'errors')}">
                              <label for="cidade"><g:message code="endereco.cidade.label" default="cidade" /></label>
                              <br/>
                              <g:select name="cidade.id" from="${Cidade.list()}" optionKey="id" value="${enderecoInstance?.cidade?.id}" optionValue="nome" />
                          </td>
                          <td class="btsalvarlateral">
                            <span class="button"><g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" /></span>
                          </td>
                      </tr>
                    </table>
                  </td>
                </tr>
            </tbody>
        </table>
    </div>
</form>
<%
if(enderecoInstanceList){
    %>
    <div class="">
        <div class="list">
            <table>
                <thead>
                    <tr>
                        <g:sortableColumn property="endereco" title="${message(code: 'endereco.endereco.label', default: 'Endereco')}" />
                        <g:sortableColumn property="bairro" title="${message(code: 'endereco.bairro.label', default: 'Bairro')}" />
                        <g:sortableColumn property="cep" title="${message(code: 'endereco.cep.label', default: 'Cep')}" />
                        <g:sortableColumn property="cidade" title="${message(code: 'endereco.cidade.label', default: 'Cidade')}" />
                        <td colspan="2" class="acao">Ações</td>
                    </tr>
                </thead>
                <tbody>
                <g:each in="${enderecoInstanceList}" status="i" var="enderecoInstance">
                    <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
                        <td>${fieldValue(bean: enderecoInstance, field: "endereco")}</td>
                        <td>${fieldValue(bean: enderecoInstance, field: "bairro")}</td>
                        <td>${fieldValue(bean: enderecoInstance, field: "cep")}</td>
                        <td>${enderecoInstance?.cidade?.nome}</td>
                        <td class="ico">
                          <a href="${createLink(controller:'endereco', action:'edit', id:enderecoInstance.id, params:[tipo:controlador])}">
                            <img src="${resource(dir:'images',file:'ico_edit.png')}" alt="Editar Item" title="Editar Item" />
                          </a>
                        </td>
                        <td class="ico">
                            <a href="${createLink(controller:'endereco', action:'delete', id:enderecoInstance.id, params:[tipo:controlador])}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Confirma aexclusão do registro?')}');">
                                <img src="${resource(dir:'images',file:'ico_delete.png')}" alt="Excluir Item" title="Excluir Item" class="formacao" action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}"  />
                            </a>
                        </td>
                    </tr>
                </g:each>
                </tbody>
            </table>
        </div>
    </div>
    <%
}
%>
